home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 12 - 1996 / 12.08 Aug 96 / 12.08 Prog Chal / PostPlus.cpp < prev   
Encoding:
C/C++ Source or Header  |  1996-06-09  |  22.6 KB  |  933 lines  |  [TEXT/R*ch]

  1. /*
  2.   31 May 1996.
  3.   Submission to MacTech Programmer's challenge for May-96.
  4.   Copyright 1996, Ernst Munter, Kanata, ON, Canada.
  5.  
  6.         "Postman's Sort"
  7.  
  8. Problem Statement
  9. -----------------
  10.   Sort an address file, using distribution sort, and avoid
  11.   any direct or disguised (indirect) comparison between
  12.   sort keys of the input records.
  13.  
  14. Solution
  15. --------
  16.   It was tempting to create a sorted index tree of the keys,
  17.   and then sort input records by comparing (descending) the
  18.   sort tree.
  19.  
  20.   However, I think that would not have been in the spirit
  21.   of the Challenge, which was, I believe, to find a fast
  22.   implementation of Bob Ramey's postman's sort algorithm.
  23.  
  24.   Hence, this solution works entirely by distributing
  25.   records into bins, or stacks, directly indexed by a single
  26.   character.
  27.  
  28.   Tabs, and end-of-record markers are converted to \x0,
  29.   the range \x1 to \x1F is used to sort numeric fields by
  30.   their length.
  31.  
  32.   Characters above 'a' are reduced by 0x20, which makes all
  33.   upper and lower case characters the same.  Coincidentally,
  34.   it also sets {|}~ equal to [\]^.
  35.  
  36.   Whether the input file fits in memory or not, the principle
  37.   of sorting is the same:
  38.  
  39.   All records are parsed into nodes and attached to stacks
  40.   based on the current sort key, for example the first letter
  41.   of country.
  42.  
  43.   Then each stack is recursively sorted on the next sort key,
  44.   i.e. the second letter of country, in the same manner.
  45.  
  46.   If this is done in-memory (MemSort) the stacks are linked
  47.   and detached from the global STACK array, before sorting
  48.   each stack.
  49.  
  50.   If the stack was too large to fit in memory, it is sent
  51.   in segments to a temporary file on disk.
  52.   The temp file then contains interleaved segments of stacks
  53.   which we keep track of in a local variable (bin).
  54.  
  55.   To continue sorting, the segments of a particular stack
  56.   are retrieved from disk, and the whole stack may now
  57.   fit in memory for final sorting.  If it does not, it
  58.   is again sorted into segments and sent to a new temp file
  59.   on disk.
  60.  
  61.   The number of temp files needed is equal to the recursion
  62.   depth of the disk sort, until a stack can be sorted to the
  63.   finish in memory.  The more memory we have, the fewer temp
  64.   files are needed.
  65.  
  66. Assumptions
  67. -----------
  68.  
  69.   tmpfile() is used to create temporary files.  Depending
  70.   on system setup, the system may run out of files to open,
  71.   and sorting will then stop.
  72.  
  73.   The order of numerical fields will be natural numerical,
  74.   that is 9, 12, 30, 100.  However, leading zero's are not
  75.   expected (or we get 41, 80, 053, 102).
  76.  
  77.   The length of a purely numeric field to be handled correctly
  78.   should not exceed 31. Longer numeric fields could get sorted
  79.   behind fields starting with punctuation.
  80.  
  81.   The longest record expected should be substantially less
  82.   than the constant MINMEM which is set to 1024, but this
  83.   limit can be relaxed.  It plays a role only when we are
  84.   very short of memory.
  85.  
  86.   All ASCII characters may occur, and will be sorted on.
  87.   Any control characters other than Tab (0x09) will be treated
  88.   as end-of-record indicators.
  89.  
  90.   The sort is not "stable", in the sense that the order of
  91.   equivalent records (differing in case only, as an example)
  92.   is not preserved.  Records are tossed on stacks and into
  93.   bins, and are frequently retrieved in reverse order.
  94.   Furthermore, running the program with different amounts
  95.   of storage may result in a different order as well (U/L case).
  96. */
  97.  
  98. #include <stdlib.h>
  99. #include <stdio.h>
  100. #include <memory.h>
  101. #include <string.h>
  102.  
  103.  
  104. #undef shortCut
  105. //#define shortCut
  106.  
  107. #define uchar unsigned char
  108. #define ushort unsigned short
  109. #define ulong unsigned long
  110.  
  111. #define NUM_KEYS     224
  112. #define MINMEM         1024
  113. #define MIN_BUFFER     512
  114. #define DEF_BUFFER_SIZE 16384
  115. #define numFields     9
  116. #define lastField     (numFields-1)
  117. #define TAB_CHAR     0x09
  118. #define END_RECORD     0x0D
  119.  
  120. typedef struct Node Node;
  121. struct     Node {
  122.   uchar*    ref;
  123.   Node*     next;
  124.   Node*     link;
  125.   ushort    fld[numFields];
  126.   ushort    len;
  127. };
  128.  
  129. typedef struct tHeader tHeader;
  130. struct     tHeader {
  131.   long     next;
  132.   int     size;
  133.   int     key;
  134. };
  135.  
  136. typedef struct Bin Bin;
  137. struct     Bin {
  138.   FILE* tf;        //temp file
  139.   long    next;        //next segment in temp file
  140.   int    bytesLeft;    //in current segment
  141.   long  fpos[NUM_KEYS];
  142. };
  143.  
  144. typedef struct OutputBuffer OutputBuffer;
  145. struct     OutputBuffer {
  146.   int    space;        //space left
  147.   int    size;        //size of buffer
  148.   uchar c[4];        //will be c[size]
  149. };
  150.  
  151. /************ 908 bytes of static variables ***************/
  152.  
  153. static     FILE*         theOutFile;
  154. static     OutputBuffer*     buffer;
  155. static     int         topKey;
  156. static     Node*         STACK[NUM_KEYS];
  157.  
  158. /**************** macros and prototypes********************/
  159.  
  160. // Output buffer functions:
  161. // The output buffer accumulates characters to be sent to
  162. // the output file.  The block size is chosen as 16K,
  163. // but reduced to 8K if less than 128K of storage is
  164. // available.
  165. // The buffer isreleased if, after many recursions storage
  166. // space is severely reduced and we need the memory
  167. // for sorting.
  168.  
  169. void     FlushBuffer();
  170. int     OpenBuffer(uchar* storage,int storageSize);
  171. void     CloseBuffer();
  172. void     BufferedWrite(uchar* text,int size);
  173. void     BufferedSend(Node* node);
  174.  
  175. // in-memory sort and parsing functions:
  176.  
  177. Node*     Link(int* numeric);
  178. int     TestNumeric(int key,uchar* text,int len);
  179. void     Dist(Node* node,int field,int offset,int test);
  180. void     MemSort(int field,int offset);
  181. int     Parse(uchar* text,Node* node,int size,int field,int offset,int test);
  182.  
  183.  
  184. // SStream functions:
  185. // SStreams are virtual files which are interleaved on a single disk file.
  186. // Each SStream may consist of several segments, linked together
  187. // through small headers (tHeader) on the disk.  The last segment
  188. // is pointed to from a "bin".
  189. // Bins administer access to these streams.
  190.  
  191. void     InitBin(Bin* bin);
  192. int     OpenBin(Bin* bin);
  193. void     ResetBin(Bin* bin);
  194. void     CloseBin(Bin* bin);
  195. int     SendToTemp(Bin* bin);
  196. int     SegmentSize(Node* node);
  197. void     Send(Node* node,FILE* f);
  198. int     SStreamSeek(Bin* bin,int key,long pos);
  199. int     SStreamRead(uchar* text0,int size,Bin* bin);
  200. long     SStreamSize(Bin* bin,int key);
  201. void     SendSStreamOut(uchar* text,int storageSize,Bin* bin,int key);
  202.  
  203. // The DiskSort function is called from PostmansSort, and calls
  204. // itself recursively. With each recursion, the calling Bin structure
  205. // must be preserved which reduces the storage available for the
  206. // called DiskSort function.  This wastage is somewhat controlled
  207. // by recovering the unused part of the Bin structure, since
  208. // keys in the high ASCII ranges would frequently not be needed.
  209.  
  210. // Bins could also have been put on the procedure stack, but this
  211. // could put a more severe limit on recursion depth. (about 1K
  212. // per Bin), depending on the environment.
  213.  
  214. int     RecoverMemory();
  215. int     DiskSort(
  216.   Bin*     inBin,
  217.   uchar* storage,
  218.   int   storageSize,
  219.   int     field,
  220.   int     offset);
  221.  
  222.  
  223. /**************************   *********************************/
  224.  
  225. #ifdef     __BORLANDC__
  226. #define OSErr int
  227. #else
  228.     pascal
  229. #endif
  230. OSErr     PostmansSort(
  231.   FILE     *inFile,          /* stream containing sort input */
  232.   FILE     *outFile,         /* stream to contain sort output */
  233.   void     *privateStorage,  /* preallocated storage for your use */
  234.   size_t storageSize      /* number of bytes of privateStorage */
  235. );
  236.  
  237. #define QUIT(rc) {CloseBin(bin); return rc;}
  238. #define EMPTY_BIN(bin,key) (bin->fpos[key]<0)
  239.  
  240.  
  241. #ifdef shortCut
  242. #define maxField 32
  243.  
  244. // The shortCut functions are used to detect the case of a single
  245. // key in a stack of records.  This means we can jump directly to
  246. // the end of the field.
  247.  
  248. // SingleKey() detects this by AND and OR of all remaining
  249. // characters in a field, across all records.  If the results
  250. // of the AND and the OR are identical, then all records have
  251. // the same characters in the rest of the field.
  252.  
  253.  
  254. int SingleKey(uchar* hi,uchar* lo,int field,int offset,int reset);
  255. void MoveSingleStack();
  256. void MoveSingleBin(Bin* bin);
  257.  
  258. #endif
  259. /**********************************************************/
  260.  
  261.  
  262. // the following #ifdef takes care of some
  263. // compiler incompatibilities
  264.  
  265. #ifdef     __BORLANDC__
  266. #define OSErr int
  267. #else
  268.     pascal
  269. #endif
  270. OSErr PostmansSort(
  271.   FILE *inFile,          /* stream containing sort input */
  272.   FILE *outFile,         /* stream to contain sort output */
  273.   void *privateStorage,  /* preallocated storage for your use */
  274.   size_t storageSize     /* number of bytes of privateStorage */
  275. ) {
  276. uchar*     storage=(uchar*)privateStorage;
  277. Bin*     bin=(Bin*)storage;
  278. uchar*  text;
  279. Node*     node;
  280.  
  281. long     fileSize;
  282. long     shortage;
  283. long    curPos;
  284. int     textSize;
  285. int     recovered;
  286. int    rc;
  287. int     nextField=lastField;
  288.  
  289. #ifdef shortCut
  290. int    skipField=0;
  291. uchar     hi[maxField];
  292. uchar     lo[maxField];
  293. #endif
  294.  
  295. // Setup storage:
  296.  
  297.   text=(uchar*)(bin+1);
  298.   theOutFile=outFile;
  299.   storageSize-=OpenBuffer(storage,storageSize);
  300.   storageSize-=sizeof(Bin);
  301.   storageSize-=sizeof(Node);
  302.   if (storageSize<MINMEM) return 10;
  303.  
  304. // Determine size of input file and read as much as possible
  305.  
  306.   curPos=ftell(inFile);
  307.   fseek(inFile,0,2);
  308.   fileSize=ftell(inFile);
  309.   fseek(inFile,curPos,0);
  310.   textSize=fread(text,1,storageSize,inFile);
  311.  
  312. // Parse input data.  Nodes grow from the top of storage
  313. // down into the text, and may cause some of it to be reloaded
  314.  
  315.   node=(Node*)(text+storageSize);
  316.   topKey=0;
  317.   shortage=Parse(text,node,textSize,nextField,0,1);
  318.   curPos=textSize-shortage;
  319.  
  320.   if (fileSize-curPos==0) {    //everything fit
  321.  
  322.     MemSort(nextField,0);
  323.  
  324.   } else {            //distribute into bins
  325.  
  326. #ifdef shortCut
  327.   if (nextField) {
  328.     skipField=SingleKey(hi,lo,nextField,0,1);
  329.   }
  330. #endif
  331.  
  332.     InitBin(bin);
  333.     do {
  334.       if (0!=(rc=SendToTemp(bin))) QUIT(rc);
  335.       fseek(inFile,curPos,0);
  336.       textSize=fread(text,1,storageSize,inFile);
  337.       node=(Node*)(text+storageSize)-1;
  338.       shortage=Parse(text,node,textSize,nextField,0,1);
  339.       curPos+=textSize-shortage;
  340.  
  341. #ifdef shortCut
  342.         if (skipField) {
  343.           skipField=SingleKey(hi,lo,nextField,0,0);
  344.         }
  345. #endif
  346.  
  347.     } while(curPos<fileSize);
  348.     if (0!=(SendToTemp(bin))) QUIT(rc);
  349.  
  350. #ifdef shortCut
  351.       if (skipField) {
  352.         MoveSingleBin(bin);
  353.         recovered=RecoverMemory();
  354.         if (0!=(rc=DiskSort(
  355.           bin,
  356.       text-recovered,
  357.       storageSize+recovered,nextField,skipField))) QUIT(rc);
  358.       } else
  359. #endif
  360.       {
  361.         recovered=RecoverMemory();
  362.         if (0!=(rc=DiskSort(
  363.           bin,
  364.       text-recovered,
  365.       storageSize+recovered,nextField,0))) QUIT(rc);
  366.       }
  367.  
  368.     CloseBin(bin);
  369.  
  370.   }
  371.  
  372.   FlushBuffer();
  373.   return 0;
  374. }
  375.  
  376. void InitBin(Bin* bin) {
  377.   memset(bin->fpos,-1,sizeof(bin->fpos));
  378.   bin->next=0;
  379.   bin->bytesLeft=0;
  380.   bin->tf=0;
  381. }
  382.  
  383. int OpenBin(Bin* bin) {
  384.   if (bin->tf==0) {
  385.     if (0==(bin->tf=tmpfile()))
  386.       return 5;    // unable to open temp file
  387.   }
  388.   return 0;
  389. }
  390.  
  391. void ResetBin(Bin* bin) {
  392.   memset(bin->fpos,-1,sizeof(bin->fpos));
  393.   bin->next=0;
  394.   bin->bytesLeft=0;
  395.   if (bin->tf) fseek(bin->tf,0,0);
  396. }
  397.  
  398. void CloseBin(Bin* bin) {
  399.   if (bin->tf) {
  400.     fclose(bin->tf);
  401.   }
  402. }
  403.  
  404. int Parse(
  405.     uchar* text,
  406.     Node* node,
  407.     int size,
  408.     int field,
  409.     int offset,
  410.     int test) {
  411.  
  412. // returns the shortage, i.e. the difference between the
  413. // end of the loaded text and the end of the last record parsed.
  414. // The shortage is caused by the fact that we load text up to
  415. // the storage limit, but then overwrite some of it by the
  416. // parsed nodes which grow downwards from the top.  In this
  417. // way we don't have to guess at the size of a typical record.
  418.  
  419. uchar*     record=text;
  420. int     xfield=0;
  421. uchar*     textEnd=text+size;
  422.   while (text<(uchar*)node) {
  423.     int c=*text++;
  424.     if (c<' ') {
  425.       if (c==TAB_CHAR) {
  426.  
  427. // mark off those fields
  428.     xfield++;
  429.     if (xfield>lastField) return 4;
  430.     node->fld[xfield]=(ushort)(text-record);
  431.       } else {
  432.  
  433. // any other CTL-char is treated as END_RECORD:
  434.     int key=*(record+node->fld[field]+offset);
  435.  
  436. // fill in fields of node and hook to STACK:
  437.     node->ref=record;
  438.     node->link=0;
  439.     node->fld[0]=0;
  440.     node->len=(ushort)(text-record);
  441.  
  442. // key is the character in the current key position
  443. // modified to
  444. //    0 if end of field,
  445. //     lower case becomes upper case
  446. //    length of field if numeric field detected
  447.  
  448.     if (node->len==1) key=0;
  449.     else if (key>='a') key-=0x20;
  450.     else if (key<' ') key=0;
  451.     else if (test)
  452.       key=TestNumeric(key,node->ref+node->fld[field],
  453.           node->fld[field+1]-node->fld[field]-1);
  454.  
  455. // keep track of the highest key value used, for future economies.
  456.  
  457.     node->next=STACK[key];
  458.     if (key>topKey) topKey=key;
  459.     STACK[key]=node--;
  460.     record=text;
  461.     xfield=0;
  462.       }
  463.     }
  464.     if (text>=textEnd)
  465.       return 0;        //no shortage
  466.   }
  467.  
  468.   return textEnd-record;
  469. }                                                     
  470.  
  471. static mms;
  472.  
  473. void MemSort(int field,int offset) {
  474. int endField=(int)STACK[0],numeric=0;
  475. Node* node;                                                  
  476.     mms++;
  477.   node=Link(&numeric);
  478.   while (node) {
  479.     Node* nextNode=node->link;
  480.     if (0==node->next) {     //no need to sort a single record
  481.       BufferedSend(node);
  482.       endField=0;
  483.     } else if (endField) {    //go to next field
  484.       int nextField=field-1;
  485.       endField=0;
  486.       if ((field==7)&&(offset>0)) nextField-=2;
  487.       Dist(node,nextField,0,1);
  488.       MemSort(nextField,0);
  489.     } else if (numeric) {     //test same key position for value
  490.       numeric--;
  491.       Dist(node,field,offset,0);
  492.       MemSort(field,offset);
  493.     } else {            //test next key position
  494.       Dist(node,field,offset+1,0);
  495.       MemSort(field,offset+1);
  496.     }
  497.     node=nextNode;
  498.   }
  499. }
  500.  
  501. Node* Link(int* numeric) {
  502. // detaches stacks under the same key from STACK[] and
  503. // links their first nodes together
  504. Node* node=0;
  505. Node* n;
  506. int key;
  507.   for (key=topKey;key>=0;key--) {
  508.     if (0 != (n=STACK[key])) {
  509.       if ((key<' ') && (key>0)) (*numeric)++;
  510.       n->link=node;
  511.       node=n;
  512.       STACK[key]=0;
  513.     }
  514.   }
  515.   topKey=0;
  516.   return node;
  517. }
  518.  
  519. void Dist(Node* node,int field,int offset,int test) {
  520. // scan all nodes in a stack and redistribute them
  521. // back on STACK[] depending on the next key value.
  522.   do {
  523.     Node*  nextNode;
  524.     int    key=*(node->ref+node->fld[field]+offset);
  525.     if (key>='a') key-=0x20;
  526.     else if (key<' ') {
  527.       key=0;
  528.       if (field==0) {    //sorting done for this node
  529.         nextNode=node->next;
  530.         node->next=0;
  531.     BufferedSend(node);
  532.     node=nextNode;
  533.     continue;
  534.       }
  535.     } else if (test) {
  536.       key=TestNumeric(key,node->ref+node->fld[field],
  537.           node->fld[field+1]-node->fld[field]-1);
  538.     }
  539.     nextNode=node->next;
  540.     if (key>topKey) topKey=key;
  541.     node->next=STACK[key];
  542.     STACK[key]=node;
  543.     node=nextNode;
  544.   } while (node);
  545. }
  546.  
  547. int TestNumeric(int key,uchar* text,int len) {
  548. // returns the field length of a numeric field,
  549. // otherwise returns key unchanged
  550. int i;
  551.   for (i=0;i<len;i++) {
  552.     int c=*text;
  553.     if (c>'9') return key;
  554.     if (c<'0') return key;
  555.     text++;
  556.   }
  557.   return len;
  558. }
  559.  
  560. int DiskSort(
  561.   Bin* inBin,
  562.   uchar* storage,
  563.   int storageSize,
  564.   int field,
  565.   int offset) {
  566.  
  567. // recursively callable version of PostmansSort()
  568.  
  569. Bin*     bin=(Bin*)storage;
  570. uchar*     text;
  571. Node*     node;
  572.  
  573. long     fileSize;
  574. int     textSize;
  575. long     shortage;
  576. long    curPos;
  577.  
  578. int     key;
  579. int    rc;
  580. int    recovered;
  581. int    topKeyIn=topKey;
  582.  
  583.  
  584. #ifdef shortCut
  585. int    skipField;
  586. uchar     hi[maxField];
  587. uchar     lo[maxField];
  588. #endif
  589.  
  590.   InitBin(bin);
  591.   storageSize-=sizeof(Bin);
  592.   storageSize-=sizeof(Node);
  593.   if (storageSize<MINMEM) {
  594.     if (buffer) {
  595.       storageSize+=buffer->size+sizeof(OutputBuffer)-4;
  596.       CloseBuffer();
  597.       if (storageSize<MINMEM) return 8;
  598.     } else return 9;
  599.   }
  600.   text=(uchar*)(bin+1);
  601.   for (key=0;key<=topKeyIn;key++) {
  602.     int nextField,nextOffset,numericTest;
  603.     if (EMPTY_BIN(inBin,key)) continue;
  604.     if (0==(fileSize=SStreamSize(inBin,key))) continue;
  605.  
  606.     nextField=field;
  607.     nextOffset=offset;
  608.  
  609. #ifdef shortCut
  610.     skipField=0;
  611. #endif
  612.  
  613.     topKey=0;
  614.     if (key==0) {        //end of field reached
  615.  
  616.       if ((nextField==7)&&(nextOffset>0)) nextField=4;
  617.  
  618.       else if (nextField==0) {  //copy whole stream
  619.     SendSStreamOut(text,storageSize,bin,0);
  620.     continue;
  621.  
  622.       } else nextField--;
  623.  
  624.       nextOffset=0;
  625.       numericTest=1;
  626.     } else if (key<' ') {    //start of a numeric field
  627.       nextOffset=0;
  628.       numericTest=0;
  629.     } else {                    //a typical field
  630.       nextOffset++;
  631.       numericTest=0;
  632.     }
  633.  
  634.     SStreamSeek(inBin,key,0);
  635.     textSize=SStreamRead(text,storageSize,inBin);
  636.     node=(Node*)(text+storageSize);
  637.  
  638.     shortage=Parse(text,node,textSize,nextField,nextOffset,numericTest);
  639.     curPos=textSize-shortage;
  640.  
  641.     if (fileSize-curPos==0) {    //sort in memory
  642.  
  643.       MemSort(nextField,nextOffset);
  644.  
  645.     } else {            //sort in temp files
  646.  
  647. #ifdef shortCut
  648.     if ((nextField!=7) || (nextOffset==0)) {
  649.       skipField=SingleKey(hi,lo,nextField,nextOffset,1);
  650.     }
  651. #endif
  652.  
  653.       do {
  654.     if (0!=(rc=SendToTemp(bin))) QUIT(rc);
  655.     SStreamSeek(inBin,key,curPos);
  656.     textSize=SStreamRead(text,storageSize,inBin);
  657.     node=(Node*)(text+storageSize)-1;
  658.     shortage=Parse(text,node,textSize,nextField,nextOffset,numericTest);
  659.     curPos+=textSize-shortage;
  660.  
  661. #ifdef shortCut
  662.         if (skipField) {
  663.           skipField=SingleKey(hi,lo,nextField,nextOffset,0);
  664.         }
  665. #endif
  666.  
  667.       } while(curPos<fileSize);
  668.       if (0!=(rc=SendToTemp(bin))) QUIT(rc);
  669.  
  670. #ifdef shortCut
  671.       if (skipField) {
  672.         MoveSingleBin(bin);
  673.     recovered=RecoverMemory();
  674.         if (0!=(rc=DiskSort(
  675.           bin,
  676.       text-recovered,
  677.       storageSize+recovered,nextField,skipField))) QUIT(rc);
  678.       } else
  679. #endif
  680.       {
  681.         recovered=RecoverMemory();
  682.         if (0!=(rc=DiskSort(
  683.           bin,
  684.       text-recovered,
  685.       storageSize+recovered,nextField,nextOffset))) QUIT(rc);
  686.       }
  687.  
  688.       ResetBin(bin);
  689.     }
  690.   }
  691.   CloseBin(bin);
  692.   return 0;
  693. }                                                      
  694.  
  695. static sst;
  696.  
  697. int SendToTemp(Bin* bin) {
  698. // send each segment (stack of equal key records) to disk,
  699. // bin->fpos[key] points to the start of the last segment
  700. // stored. The segment header points to the previous
  701. // segment or -1 if this is the first.
  702.  
  703. int key,rc;
  704. Node* node;
  705.   if (0!=(rc=OpenBin(bin))) return rc;
  706.   for (key=0;key<NUM_KEYS;key++) {
  707.     if (0 != (node=STACK[key])) {
  708.       tHeader th;
  709.       long pos;
  710.       STACK[key]=0;
  711.       th.size=SegmentSize(node);
  712.       th.next=bin->fpos[key];
  713.       th.key=key+('#'<<8);
  714.       pos=ftell(bin->tf);
  715.       fwrite(&th,1,sizeof(tHeader),bin->tf);     
  716.       sst++;
  717.       bin->fpos[key]=pos;
  718.       Send(node,bin->tf);
  719.     }
  720.   }
  721.   return 0;
  722. }
  723.  
  724. int SegmentSize(Node* node) {
  725. // The length of a segment needs to be known for the header,
  726. // before we store the text records.  This functions scans
  727. // through all nodes of a stack and adds up their lengths.
  728. int s=0;
  729.   do {
  730.     s+=node->len;
  731.     node=node->next;
  732.   } while(node);
  733.   return s;
  734. }
  735.  
  736. void Send(Node* node,FILE* f) {
  737. // Sends a stack to disk, either the temp file, or
  738. // the output file in the case of unbuffered output.
  739.   do {
  740.     fwrite(node->ref,1,node->len,f);
  741.     node=node->next;
  742.   } while(node);
  743. }
  744.  
  745. int SStreamSeek(Bin* bin,int key,long pos) {
  746. // returns error=7 if no SStream, or if seeked beyond size
  747. long fpos=bin->fpos[key];
  748. tHeader th;
  749.   if (0==bin->tf) return 6;
  750.   while (fpos>=0) {
  751.     fseek(bin->tf,fpos,0);
  752.     fread(&th,1,sizeof(th),bin->tf);
  753.     if (pos<th.size) {
  754.       fseek(bin->tf,pos,1);
  755.       bin->bytesLeft=th.size-pos;
  756.       bin->next=th.next;
  757.       return 0;
  758.     } else {
  759.       pos-=th.size;
  760.       fpos=th.next;
  761.     }
  762.   }
  763.   return 7;
  764. }
  765.  
  766. int SStreamRead(uchar* text0,int size,Bin* bin) {
  767. // reads from current position reached with SStreamSeek()
  768. // returns the actual number of bytes read
  769. long fpos;
  770. tHeader th;
  771. int bytes2read;
  772. uchar* text=text0;
  773.   if (bin->bytesLeft) {
  774.     if (bin->bytesLeft>size) bytes2read=size;
  775.     else bytes2read=bin->bytesLeft;
  776.     fread(text,1,bytes2read,bin->tf);
  777.     text+=bytes2read;//even if fread comes short
  778.     size-=bytes2read;
  779.   }
  780.   fpos=bin->next;
  781.   while ((size) && (fpos>=0)) {
  782.     fseek(bin->tf,fpos,0);
  783.     fread(&th,1,sizeof(th),bin->tf);
  784.     if (th.size>size) bytes2read=size;
  785.     else bytes2read=th.size;
  786.     fread(text,1,bytes2read,bin->tf);
  787.     text+=bytes2read;//even if fread comes short
  788.     size-=bytes2read;
  789.     fpos=th.next;
  790.   }
  791.   return text-text0;
  792. }
  793.  
  794. long SStreamSize(Bin* bin,int key) {
  795. // determines the total SStream size by adding up the
  796. // sizes of all its segments.
  797. long fpos=bin->fpos[key];
  798. tHeader th;
  799. long size=0;
  800.   while (fpos>=0) {
  801.     fseek(bin->tf,fpos,0);
  802.     fread(&th,1,sizeof(th),bin->tf);
  803.     size+=th.size;
  804.     fpos=th.next;
  805.   }
  806.   return size;
  807. }
  808.  
  809. void SendSStreamOut(uchar* text,int storageSize,Bin* bin,int key) {
  810. // function to copy an entire SStream to the output file, which
  811. // can happen when there is a large number of identical records.
  812. long fpos=bin->fpos[key];
  813. tHeader th;
  814.   while (fpos>=0) {
  815.     fseek(bin->tf,fpos,0);
  816.     fread(&th,1,sizeof(th),bin->tf);
  817.     while(th.size){
  818.       int textSize=storageSize;
  819.       if (textSize>th.size) textSize=th.size;
  820.       fread(text,1,textSize,bin->tf);
  821.       BufferedWrite(text,textSize);
  822.       th.size-=textSize;
  823.     }
  824.     fpos=th.next;
  825.   }
  826. }
  827.  
  828. #ifdef shortCut
  829.  
  830. int SingleKey(uchar* hi,uchar* lo,int field,int offset,int reset) {
  831. int key;
  832. Node* node;
  833. int endField;
  834. int i;
  835.   for (key=0;key<topKey;key++) {
  836.     if (STACK[key]) return 0;
  837.   }
  838.   node=STACK[topKey];
  839.   endField=node->fld[field+1]-node->fld[field]-offset-1;
  840.   if (endField==0) return 0;
  841.   if (endField>=maxField) return 0;
  842.  
  843.   if (reset) for (i=0;i<=endField;i++) {hi[i]=0;lo[i]=0xFF;}
  844.  
  845.   while (node) {
  846.     uchar* f=node->ref+node->fld[field]+offset;
  847.     for (i=0;i<=endField;i++) {
  848.       uchar c=*f++;
  849.       lo[i] &= c;
  850.       hi[i] |= c;
  851.       if (hi[i] != lo[i]) return 0;
  852.     }
  853.     node=node->next;
  854.   }
  855.   if (hi[endField] >= ' ') return 0;
  856.   return endField;
  857. }
  858.  
  859. void MoveSingleStack() {
  860.   STACK[0]=STACK[topKey];
  861.   STACK[topKey]=0;
  862.   topKey=0;
  863. }
  864.  
  865. void MoveSingleBin(Bin* bin) {
  866.   bin->fpos[0]=bin->fpos[topKey];
  867.   bin->fpos[topKey]=-1;
  868.   topKey=0;
  869. }
  870.  
  871. #endif
  872.  
  873. int RecoverMemory() {
  874. // unused top part of the current bin
  875.   return (NUM_KEYS-topKey-1)*sizeof(long);
  876. }
  877.  
  878. void FlushBuffer() {
  879. int bytes2send=buffer->size-buffer->space;
  880.   if (bytes2send) {
  881.     fwrite(buffer->c,1,bytes2send,theOutFile);
  882.   }
  883.   buffer->space=buffer->size;
  884. }
  885.  
  886. int OpenBuffer(uchar* storage,int storageSize) {
  887. // allocates 1/8 of available memory, up to a maximum of
  888. // DEF_BUFFER_SIZE as output buffer for outFile
  889.  
  890. int maxSize=storageSize>>3;
  891. int bufferSize=DEF_BUFFER_SIZE;
  892.   while (bufferSize>maxSize) bufferSize>>=1;
  893.   if (bufferSize>=MIN_BUFFER) {
  894.     buffer=(OutputBuffer*)(storage+storageSize-
  895.       (bufferSize+sizeof(OutputBuffer)-4));
  896.     buffer->size=bufferSize;
  897.     buffer->space=bufferSize;
  898.     return bufferSize+sizeof(OutputBuffer)-4;
  899.   } else return 0;
  900. }
  901.  
  902. void CloseBuffer() {
  903.   FlushBuffer();
  904.   buffer=0;
  905. }
  906.  
  907. void BufferedWrite(uchar* text,int size) {
  908.   if (buffer) {
  909.     if (size > buffer->space) {
  910.       int n1=buffer->space;
  911.       int n2=size-buffer->space;
  912.       memcpy(buffer->c+buffer->size-buffer->space,text,n1);
  913.       buffer->space=0;
  914.       FlushBuffer();
  915.       BufferedWrite(text+n1,n2);
  916.     } else {
  917.       memcpy(buffer->c+buffer->size-buffer->space,text,size);
  918.       buffer->space-=size;
  919.     }
  920.   } else {
  921.     fwrite(text,1,size,theOutFile);
  922.   }
  923. }
  924.  
  925. void BufferedSend(Node* node) {
  926.   if (buffer) {
  927.     do {
  928.       BufferedWrite(node->ref,node->len);
  929.       node=node->next;
  930.     } while(node);
  931.   } else Send(node,theOutFile);
  932. }
  933.